package com.panjing.ar.Common;


import com.cmcc.ar.R;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MovieView extends RelativeLayout {
	
	private SurfaceView surfaceView;
	private Button btnPause, btnPlayUrl, btnStop;
	private SeekBar skbProgress;
	private Player player;
	private ProgressDialog loadingDialog;
	private TextView textDis;
	private RelativeLayout timeDis;
	private TextView timeText;
	private boolean disFlag = false;
//	private Matrix matrix = null;
	private int controlTime = -1;
	private String controlContent = null;
	
	public MovieView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		inflate(context,R.layout.movie_layout,this);
		surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);
//		surfaceView.setZOrderOnTop(true);
		
		Log.d("movieView", "+++++++++++++++++onCreate");
		btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
		btnPlayUrl.setOnClickListener(new ClickEvent());

		btnPause = (Button) this.findViewById(R.id.btnPause);
		btnPause.setOnClickListener(new ClickEvent());

		btnStop = (Button) this.findViewById(R.id.btnStop);
		btnStop.setOnClickListener(new ClickEvent());

		skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
		skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
		player = new Player(surfaceView, skbProgress);
		
		textDis = (TextView) this.findViewById(R.id.text_dis);
		textDis.setOnClickListener(new ClickEvent());
		textDis.bringToFront();
		
		timeDis = (RelativeLayout)this.findViewById(R.id.random_dis);
		timeText = (TextView) this.findViewById(R.id.text_random);
		
		loadingDialog = new ProgressDialog(context);
		
	}
	
//	@Override
//	protected void onDraw(Canvas canvas) {
//	     canvas.rotate(30, this.getWidth()/2,this.getHeight()/2);
//	     super.onDraw(canvas);
//	}
	
//	public void setMatixValues(float[] values){
//		matrix.setValues(values);
//	}
	
	public void setControlTime(int time,String content,String link){
		controlTime = time;
		controlContent = content;
		timeText.setText(controlContent);
		textDis.setText(link);
	}
	
	class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			if (arg0 == btnPause) {
				player.pause();
			} else if (arg0 == btnPlayUrl) {
//				String url = "http://daily3gp.com/vids/family_guy_penis_car.3gp";
//				player.playUrl(url);
				timeDis.setVisibility(View.GONE);
				disFlag = false;
				player.play();
			} else if (arg0 == btnStop) {
				player.stop();
			}else if(arg0 == textDis){
								
			}else if(arg0 == timeDis){
				
			}
		}
	}
	
	public void startMovie(String url){
		player.setPlayUrl(url,handleloading);
		disFlag = false;
		timeDis.setVisibility(View.GONE);
		surfaceView.setVisibility(View.VISIBLE);
		surfaceView.setZOrderOnTop(true);
	}
	
	public void stopMovie(){
		player.stop();
		surfaceView.setVisibility(View.GONE);
		skbProgress.setProgress(0);
		skbProgress.setSecondaryProgress(0);
		handleloading.sendEmptyMessage(200);
	}
	
	private Handler handleloading = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 100:
				if(MovieView.this.getVisibility() == View.VISIBLE){
					loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					loadingDialog.setIndeterminate(false);
					loadingDialog.setCancelable(true);
					loadingDialog.setMessage("视频加载中，请稍后！");
					loadingDialog.show();
				}
				break;
			case 200:
				loadingDialog.dismiss();
				break;
			case 300:
				loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				loadingDialog.setIndeterminate(false);
				loadingDialog.setCancelable(true);
				loadingDialog.setMessage("您尚未设置视频。。。。");
				loadingDialog.show();
				break;
			case 400:
				loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				loadingDialog.setIndeterminate(false);
				loadingDialog.setCancelable(true);
				loadingDialog.setMessage("视频加载失败，请重新加载。。。。");
				loadingDialog.show();
				break;
			default:
				break;
			}
		};
	};
	
	class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
		int progress;

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
			
			if(player.mediaPlayer != null){
				this.progress = progress * player.mediaPlayer.getDuration()
						/ seekBar.getMax();
				
				Log.d("MovieView","the progress is "+this.progress);
				
				int contentDisplayTime = player.mediaPlayer.getCurrentPosition();
				
				if(controlTime > 0 && controlContent != null){
					if(contentDisplayTime > controlTime && !disFlag){
						timeDis.setVisibility(View.VISIBLE);
						disFlag = true;
					}
					
					if(contentDisplayTime > controlTime + 5000 && disFlag){
						timeDis.setVisibility(View.GONE);
						disFlag = false;
					}
				}
				
			}else{
				this.progress = 0;
			}

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// seekTo()的参数是相对与影片时间的数字，而不是与seekBar.getMax()相对的数字
			player.mediaPlayer.seekTo(progress);
		}
	}

	

}
